package com.jiangyg.mall.core.support.validation;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import java.io.Serializable;

/**
 * 类描述：验证错误
 *
 * @author jiangyg
 * @date 2020-08-01
 * @version 2.0
 */
@JsonPropertyOrder({"type", "field", "message"})
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ValidationError implements Serializable {

    private static final long serialVersionUID = 3068949586030983916L;

    /**
     * 错误类型，1-字段校验错误 2-对象校验错误
     */
    private String type;

    /**
     * 发生错误字段
     */
    private String field;

    /**
     * 错误信息
     */
    private String message;

    public ValidationError() {
        super();
    }

    /**
     * 功能描述：创建字段验证错误
     *
     * @param field   字段名称
     * @param message 错误信息
     * @return 验证错误信息
     * @author jiangyg
     * @date 2020-12-29
     */
    public static ValidationError buildFiledError(String field, String message) {
        final ValidationError error = new ValidationError();
        error.setType("1");
        error.setField(field);
        error.setMessage(message);
        return error;
    }

    /**
     * 功能描述：创建对象验证信息
     *
     * @param message 错误信息
     * @return 验证错误信息
     * @author jiangyg
     * @date 2020-12-29
     */
    public static ValidationError buildObjectError(String message) {
        final ValidationError error = new ValidationError();
        error.setType("2");
        error.setMessage(message);
        return error;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return "ValidationError [type=" + type + ", field=" + field + ", message=" + message + "]";
    }

}
